﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL.BusinessRules
{
    public class ValidateRange : BusinessRule
    {
        private ValidationOperator Operator { get; set; }
        private ValidationDataType DataType { get; set; }
        private object Min { get; set; }
        private object Max { get; set; }

        public ValidateRange(string propertyName, object min, object max, ValidationOperator @operator, ValidationDataType dataType)
            : base(propertyName)
        {
            Min = min;
            Max = max;
            Operator = @operator;
            DataType = dataType;
            ErrorMessage = string.Format("{0} must be between {1} and {2}", propertyName, min, max);
        }

        public ValidateRange(string propertyName, string errorMessage, object min, object max, ValidationOperator @operator, ValidationDataType dataType)
            : this(propertyName, min, max, @operator, dataType)
        {
            ErrorMessage = errorMessage;
        }

        public override bool Validate(BusinessObject businessObject)
        {
            try
            {
                string value = GetPropertyValue(businessObject).ToString();
                switch (DataType)
                {
                    case ValidationDataType.String:
                        string sMin = Min.ToString();
                        string sMax = Max.ToString();

                        int result1 = string.Compare(sMin, value);
                        int result2 = string.Compare(sMax, value);

                        return (result1 >= 0 && result2 <= 0);
                    case ValidationDataType.Integer:
                        int iMin = int.Parse(Min.ToString());
                        int iMax = int.Parse(Max.ToString());
                        int iValue = int.Parse(value);
                        return (iValue >= iMin && iValue <= iMax);
                    case ValidationDataType.Double:
                        double dMin = Double.Parse(Min.ToString());
                        double dMax = double.Parse(Max.ToString());
                        double dValue = double.Parse(value);
                        return (dValue >= dMin && dValue <= dMax);
                    case ValidationDataType.Decimal:
                        decimal nMin = decimal.Parse(Min.ToString());
                        decimal nMax = decimal.Parse(Max.ToString());
                        decimal nValue = decimal.Parse(value);
                        return (nValue >= nMin && nValue <= nMax);
                    case ValidationDataType.Date:
                        DateTime tMin = DateTime.Parse(Min.ToString());
                        DateTime tMax = DateTime.Parse(Max.ToString());
                        DateTime tValue = DateTime.Parse(value);
                        return (tValue >= tMin && tValue <= tMin);
                }
                return false;
            }
            catch { return false; }
        }
    }
}
